#include "info.h"

int main(int argc, char const *argv[])
{
    
    while(1)
    {   
        init_lcd(&lcdinfo);//初始化lcd
        print_init();//初始界面
        int touch =  touchPanel();

        if(touch == 2)//左滑进入标牌
        {
            while(1)
            {
                pthread_t t1[5];
                pthread_create(&t1[0], NULL, content1, NULL);
                pthread_create(&t1[1], NULL, content2, NULL);
                pthread_create(&t1[2], NULL, content3, NULL);
                pthread_create(&t1[3], NULL, content4, NULL);
                pthread_create(&t1[4], NULL, days, NULL);
                // pause();

                while(1)
                {
                    int touch = touchPanel();
                    if(touch == 3)
                    {   
                        for(int i = 0; i < 5; i++)
                        {
                            // pthread_detach(t1[i]);   
                            pthread_cancel(t1[i]);                                                      
                        }
                        break;                   
                    }                    
                }
            break;
            }
        }

        if(touch == 1)//右滑进入相册
        {
            // 1. 准备好LCD
            init_lcd(&lcdinfo);
            bitmap_list head = init_bitmap();//初始化头节点

            // 打开目录
            DIR *dp = opendir(argv[1]);
            if (!dp) 
            {  
                perror("opendir");  
                return 1;  
            } 
            // 进入目录
            chdir(argv[1]);

            struct dirent *ep;
            while(1)
            {
                ep = readdir(dp); // 目录项（文件）指针
                if(ep == NULL)
                    break;

                // 判断是否是bmp文件
                if(strstr(ep->d_name, ".bmp"))
                {
                    FILE *fp = fopen(ep->d_name, "rb");
                    if (!fp) 
                    {  
                        perror("fopen");  
                        continue;  
                    }

                    bitmap_list new = new_node(fp,ep);

                    strcpy(new->name,ep->d_name);//文件名称
                    add_bitmap_node(head,new);//添加图片信息节点
                    fclose(fp);
                }
                // 判断是否是jpg文件并加载信息
                else if(strstr(ep->d_name,".jpg"))
                {
                    FILE *fp = fopen(ep->d_name, "r");
                    if (!fp) 
                    {  
                        perror("fopen");  
                        continue;  
                    }

                    bitmap_list new = calloc(1,sizeof(bitmap1));  
                    new->prve = new;
                    new->next = new;
                    new->fp = fp;

                    strcpy(new->name,ep->d_name);//记录文件名称     
                    load_jpg(new->name, new);//加载jpg信息        
                    add_bitmap_node(head,new);//添加图片信息节点

                    fclose(fp);
                }
            }
            show(head);
            show_init(head->next,0);
            bitmap_list p;
            p=head->next;
            int num=0;
            while(1)
            {
                num = ++num % 2;
                int touch = touchPanel();//1左2右

                if(touch)
                {
                    if(touch == 1)
                    {
                        p = p->prve;
                        if(p == head)
                            p = p->prve;
                        printf("%s:%d*%d\n",p->name,p->width,p->height);
                        
                        show_init(p,num);
                    }
                    if(touch == 2)
                    {
                        p = p->next;
                        if( p == head)
                            p = p->next;
                        printf("%s:%d*%d\n",p->name,p->width,p->height);

                        show_init(p,num);
                    }
                    if(touch == 3)
                    {
                        // del_node(head);
                        break;
                    }            
                }
            }
        }
    }

    return 0;
}
